use ../Data/Clean/cleaned_recruiter_data.dta, clear
drop if hiringmanagerisempty==1
egen hiring_managertypes=group(hmw hmm hme )
egen candidate_types=group(cw cm ce cb)
gen tempvar=-pacceptjoboffer
sort candidateid tempvar, stable
by candidateid: gen rank=_n
gen t1=pacceptinterview
* replace t1=rank
keep t1 screenerid candidateid
reshape wide t1, i(screenerid) j(candidateid)

set matsize 11000
unab stubs : t1*
unab stubs2 : t1*
foreach var in `stubs' {
        quietly {
                local tocorr ""
                foreach var2 in `stubs2' {
                        disp "`var' `var2'"
                        capture drop nomiss
                        gen nomiss=!missing(`var') & !missing(`var2')
                        sum nomiss if nomiss==1
                        if (r(N)>1) {
                                cor `var' `var2'
                                if (!missing(r(rho))) {
                                                local tocorr="`tocorr' `var2'"
                                        }
                        }
                }
                drop nomiss
                if (length("`tocorr'")>0) {
                        estpost corr `var' `tocorr'
                        eststo `var'
                        esttab `var' using ../Data/temp/`var'-temp-cross-emp-correlations.csv, unstack compress b(3) nostar not nonum replace noobs mtitles() varlabels() plain
                        shell sed -i -e "s/b//g" "../Data/temp/`var'-temp-cross-emp-correlations.csv"
                        eststo drop `var'
                        local stubs2=subinstr("`stubs2'","(`var')","",.)
                }
        }
        disp "`var'"
}

local i=0
cap erase ../Data/Temp/correlated-temp.dta
foreach var in `stubs' {
	drop _all
  capture confirm file "../Data/temp/`var'-temp-cross-emp-correlations.csv"
  if _rc==0 {
		insheet using "../Data/temp/`var'-temp-cross-emp-correlations.csv", comma names clear
		drop if _n==1
		if `i'>0 append using ../Data/Temp/correlated-temp-emp.dta
		save ../Data/Temp/correlated-temp-emp.dta, replace
		local i=1
	}
	else {
    display "The file ../Data/temp/`var'-temp-cross-emp-correlations.csv does not exist"
  }
}

use ../Data/Temp/correlated-temp-emp.dta, clear
rename v1 left_eid
destring t*, replace
collapse (max) t1* , by(left_eid )

reshape long t1, i(left_eid) j(right_eid) string
replace left_eid=subinstr(left_eid, "t1", "",.)
rename t1 t1corr
drop if missing(t1corr) | left_eid==right_eid

gen pair_id=left_eid+"-"+right_eid
replace pair_id=right_eid+"-"+left_eid if right_eid>left_eid

* Dropping duplicates
bysort pair_id: gen dropme=_n
drop if dropme==2
drop dropme

*save ../Data/correlated-preferences-top1-emp.dta, replace

sum t1corr, det
* The upper left quadrant of Table 3 is produced by this code and manually placed into the table. 
